package com.itany.ems.servlet;


import com.itany.ems.entity.Emp;
import com.itany.ems.exception.RequestParameterErrorException;
import com.itany.ems.exception.ServiceException;
import com.itany.ems.service.EmpService;
import com.itany.ems.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("*.emp")
public class EmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=uft-8");
        String path = req.getServletPath();
        if("/add.emp".equals(path)){
            addEmp(req,resp);
        }
        if("/findAll.emp".equals(path)){
            findAll(req, resp);
        }
        if("/remove.emp".equals(path)){
            removeEmp(req,resp);
        }
        if("/modify.emp".equals(path)){
            modifyEmp(req,resp);
        }
    }

    private void removeEmp(HttpServletRequest req, HttpServletResponse resp) {
        EmpService service=new EmpServiceImpl();
        String id = req.getParameter("id");
        try {
            service.removeEmp(Integer.parseInt(id));
            resp.sendRedirect(req.getContextPath()+"/findAll.emp");
        }  catch (ServiceException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        EmpService service=new EmpServiceImpl();
        HttpSession session = req.getSession();
        try {
            List<Emp> emps = service.findAll();
            session.setAttribute("emps",emps);
            req.getRequestDispatcher("/jsp/empList.jsp").forward(req,resp);
        }  catch (ServletException e) {
            e.printStackTrace();
        }
    }

    private void addEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmpService service=new EmpServiceImpl();
        String name = req.getParameter("name");
        String salary = req.getParameter("salary");
        String gender = req.getParameter("gender");
        String edu = req.getParameter("edu");
        String[] hobbies=req.getParameterValues("hobbies");

        try {
            service.Register(name,salary,gender,edu,hobbies);
            resp.sendRedirect(req.getContextPath()+"/findAll.emp");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            req.getRequestDispatcher("/jsp/addEmp.jsp").forward(req,resp);
        } catch (ServiceException e) {
            e.printStackTrace();
            req.getRequestDispatcher("/jsp/addEmp.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("logMsg","服务器异常");
            req.getRequestDispatcher("/jsp/addEmp.jsp").forward(req,resp);
        }
    }

    private void modifyEmp(HttpServletRequest req, HttpServletResponse resp) {
        EmpService service=new EmpServiceImpl();
    }
}
